package com.omterra.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class OmterraGame extends ApplicationAdapter implements InputProcessor {
    
    private SpriteBatch batch;
    private Texture img;
    private OrthographicCamera camera;
    private Sprite sprite;
    private TmxMapLoader mapLoader;
    private TiledMap tiledMap;
    private TiledMapRenderer mapRender;
    
    private final int VIRTUAL_WIDTH = 1920;
    private final int VIRTUAL_HEIGHT = 1080;
	
    @Override
    public void create () {
        this.mapLoader = new TmxMapLoader();
        this.tiledMap = this.mapLoader.load("Levels/TestLevel.tmx");
        this.mapRender = new OrthogonalTiledMapRenderer(tiledMap);
        
        batch = new SpriteBatch();
        
        img = new Texture("badlogic.jpg");
        sprite = new Sprite(img);
        sprite.setOrigin(0, 0);
        sprite.setPosition(0, 0);
        
        camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
        camera.update();
        
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render () {
        camera.update();
        
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        mapRender.setView(camera);
        mapRender.render();
        
//        batch.setProjectionMatrix(camera.combined);
//        batch.begin();
//        sprite.draw(batch);
//        batch.end();
    }
    
    @Override
    public void resize(int width, int height) {
        camera.setToOrtho(false, width, height);
        camera.update();
    }
    
    @Override
    public void dispose() {
        super.dispose();
        this.tiledMap.dispose();
        this.img.dispose();
        this.batch.dispose();
    }
    
    @Override
    public boolean keyUp(int keycode) {
        if(keycode == Input.Keys.LEFT)
            camera.translate(-32,0);
        if(keycode == Input.Keys.RIGHT)
            camera.translate(32,0);
        if(keycode == Input.Keys.UP)
            camera.translate(0,-32);
        if(keycode == Input.Keys.DOWN)
            camera.translate(0,32);
        return false;
    }

    @Override
    public boolean keyDown(int i) {
        return false;
    }

    @Override
    public boolean keyTyped(char c) {
        return false;
    }

    @Override
    public boolean touchDown(int i, int i1, int i2, int i3) {
        return false;
    }

    @Override
    public boolean touchUp(int i, int i1, int i2, int i3) {
        return false;
    }

    @Override
    public boolean touchDragged(int i, int i1, int i2) {
        return false;
    }

    @Override
    public boolean mouseMoved(int i, int i1) {
        return false;
    }

    @Override
    public boolean scrolled(int i) {
        return false;
    }

}
